package com.lhh.adminlte.base;

import com.lhh.adminlte.dao.UserMapper;
import com.lhh.adminlte.entity.Menu;
import com.lhh.adminlte.entity.User;
import com.lhh.adminlte.entity.UserExample;
import com.lhh.adminlte.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Auther: hanhui.liu
 * @Date: 2018/5/10 10:38
 * @Description:
 */
public class MyRealm extends AuthorizingRealm {
    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        LTEUser lteUser = (LTEUser) principals.getPrimaryPrincipal();
        if (lteUser.getCurrentRoleId() != null) {
            List<Menu> menus = userService.findMenusByRoleId(lteUser.getCurrentRoleId(), null);
            for (Menu menu : menus) {
                if (menu.getLevel() == null || menu.getLevel() == 0) {
                    List<Menu> tempMenus = userService.findChildrenByParentId(menu.getId(), null);
                    for (Menu tempMenu : tempMenus) {
                        authorizationInfo.addStringPermission(tempMenu.getUrl());
                    }
                } else {
                    authorizationInfo.addStringPermission(menu.getUrl());
                }
            }
        }
        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToke = (UsernamePasswordToken) token;
        String loginName = usernamePasswordToke.getUsername();
        String password = new String(usernamePasswordToke.getPassword());
        password = new Md5Hash(password).toString();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginnameEqualTo(loginName);
//        criteria.andIdEqualTo(2);
        criteria.andPasswordEqualTo(password);
        List<User> userList = userMapper.selectByExample(userExample);
        if (userList != null && userList.size() == 1) {
            usernamePasswordToke.setPassword(password.toCharArray());
            LTEUser lteUser = new LTEUser();
            User user = userList.get(0);
            lteUser.setId(user.getId());
            lteUser.setNickName(user.getNickname());
            lteUser.setLoginName(user.getLoginname());
            return new SimpleAuthenticationInfo(lteUser, password, getName());
        } else {
            throw new IncorrectCredentialsException();
        }
    }
}
